#include "udp_libevent.h"
#include "tcp_server_libevent.h"
#include "winsock2.h"
#include <stdio.h>
#include <thread>
#include <stdlib.h>
#include <functional>


int main(){
    WORD wdVersion = MAKEWORD(2, 2);   //定义自己需要的网络库版本，这里是2.2
    WSADATA wdSockMsg;//这是一个结构体
    int nRes = WSAStartup(wdVersion, &wdSockMsg);//打开一个套接字
	struct sockaddr_in local_addr;

	memset(&local_addr, 0, sizeof(local_addr));
	local_addr.sin_family = AF_INET;
	local_addr.sin_port = htons(9511);
	local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    if (0 != nRes) {
        switch (nRes) {
            case WSASYSNOTREADY:
                printf("check your library");
            break;
            case WSAVERNOTSUPPORTED:
                printf("need updated");
                break;
            case WSAEINPROGRESS:
                printf("need reboot");
                break;
            case WSAEPROCLIM:
                printf("WSAEPROCLIM");
                break;
        }
    }
    if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion)) {
        printf("WSACleanup");
        WSACleanup();
        return 0;
    }
    UdpDataGramLibevent *p = new UdpDataGramLibevent("0.0.0.0",9516);
    UdpDataGramLibevent::OnReadDataHandle onReadCallback = 
        [](const char *dat,int len,struct sockaddr_in cliaddr){
            std::cout<<"recv data len "<< len << "  "<< dat<<"  from  "<<cliaddr.sin_port <<std::endl;
        };
    p->SetOnReadHandle(onReadCallback);
    while(1){
        Sleep(1000);
        // p->SendTo("123456",5,"127.0.0.1",9511);
    }
}